package org.umlviewer.model.usecase_diagram;

import java.awt.*;
import java.awt.geom.Point2D;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.umlviewer.model.DiagramElement;
import org.umlviewer.model.Constants;
import org.umlviewer.paint.PaintUtils;

/**
 *
 * @author Rafa
 */
public class UseCase extends DiagramElement{

    private int XRADIO=160;
    private final int DEFAULTX = 160;
    private int YRADIO=80;
    
    public UseCase()
    {
        setName("");
//        setFillColor(Color.white);
//        setLineColor(Color.red);
    }
    
    public UseCase(String name)
    {
        setName(name);
//        setFillColor(Color.white);
//        setLineColor(Color.black);
    }
    
    private int getMinWidth(){
        return getName().length() * Constants.SLETTER + 50;
    }
    
    private int getMinHeight(){
        return 80;
    }
    
    @Override
    public void paint(Graphics g2) {
        Graphics2D g = (Graphics2D)g2;                
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);        
        
        if(Needrefresh())
        {
            XRADIO = getName().length() * Constants.SLETTER + 50;
            if(DEFAULTX >= XRADIO)
                XRADIO = DEFAULTX;
            setRefresh(false);
        }
        
        //ovalo
        g.setColor(getLineColor());
        g.drawOval(getPosX(), getPosY(), XRADIO, YRADIO);
        //alto y ancho del rectangulo
        setWidth(XRADIO);
        setHeight(YRADIO);
        //pintar el fondo
        g.setColor(getFillColor());
        g.fillOval(getPosX()+1, getPosY()+1, XRADIO-1, YRADIO-1);
        //nombre
        g.setColor(Constants.DEFAULTCOLOR);
        g.setFont(Constants.boldFont);
        //centrar el nombre
        PaintUtils.drawCenterString(g, getName(), getPosX(), getPosY() + YRADIO/2, getWidth());
            
        g.setFont(Constants.defaultFont);
        
        if(isSelected())
            super.paintSelected(g);
        
    }
    
    @Override
    public Point2D getRelPoint(Point away){
        Point2D ret = null;
        double dstX, dstY;
        double srcX, srcY;              
         //punto medio inicial
        dstX = this.getPosX() + this.getWidth()/2;
        dstY = this.getPosY() + this.getHeight()/2;
        //punto final 
        srcX = away.x;
        srcY = away.y;
        
        double angle = Math.atan2((double) (srcX - dstX),
                        (double) (srcY - dstY));
        dstX = ((Math.sin(angle) * this.getWidth()/2)) + dstX;
        dstY = ((Math.cos(angle) * this.getHeight()/2)) + dstY;        
        
        ret = new Point2D.Double(dstX,dstY);
        return ret;
    }
    
    @Override
    public void resize(Rectangle r){
        setPosX(r.x);
        setPosY(r.y);
        if(r.width > getMinWidth())
            XRADIO = r.width;
        if(r.height > getMinHeight())
            YRADIO = r.height;
    }

     public Element xmiSerialize(){
        Namespace xmins = Constants.XMINAMESPACE;
        Element caso = new Element("ownedMember");
        caso.setAttribute("type","uml:UseCase",xmins);
        caso.setAttribute("id",getId(),xmins);
        caso.setAttribute("name",getName());
        caso.setAttribute("visibility","public");
        return caso;
    }

    
}
